import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;


public class TCPServer {
	public static void main(String argv[]) throws Exception
    {
       String clientSentence;
       String capitalizedSentence;
       ServerSocket welcomeSocket = new ServerSocket(6789);
       System.out.println("Socket criado");
       
       while(true)
       {
    	  System.out.println("SERVER: Aguardando conexão");
          Socket connectionSocket = welcomeSocket.accept();
          System.out.println("SERVER: Conexão estabelecida");
          BufferedReader inFromClient =
             new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
          
          System.out.println("SERVER: Recebendo dados...");
          DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
          clientSentence = inFromClient.readLine();
          
          System.out.println("Received: " + clientSentence);
          capitalizedSentence = clientSentence.toUpperCase() + '\n';
          outToClient.writeBytes(capitalizedSentence);
       }
    }
}
